package com.enclose;

import java.sql.*;

public class JDBC {
    /*
    1.创建驱动
    2.得到comment
    3.statement
        1）PreparedStatement:直接需要sql
        2）Statement：不需要
    4.rerult
        1）执行
        2）得到结果
    5.关闭
     */
    private static Connection connection = null;
    private  Statement statement;
    private  ResultSet resultSet;
    static {
        //创建驱动
       //java开发
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xiaomistroy?userSSL=false&serverTimezone=Asia/Shanghai","root","123456");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

//          2）Statement：不需要
    public Statement getStatement(){
        try {
            Statement statement1 = connection.createStatement();
            return statement1;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    //直接获取PreparedStatement
    public PreparedStatement preparedStatement(String sql){

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            return preparedStatement;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        1）PreparedStatement:直接需要sql
    }


    //5.关闭空间
    public void close(Statement statement,ResultSet resultSet){
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



}
